//
//  OPZJFatherTableViewController.m
//  NewWorld
//
//  Created by 曾静 on 16/4/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "OPZJFatherTableViewController.h"
#import "OPZJFatherTableViewCell.h"
@interface OPZJFatherTableViewController ()

@property (nonatomic, strong)NSArray  *dataArray;
@end

@implementation OPZJFatherTableViewController
-(instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick:)];
    
    //设置左边的按钮
    self.navigationItem.leftBarButtonItem  = leftItem;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *forthDic  =  self.dataArray[indexPath.section][@"items"][indexPath.row];
    //1.获取 跳转控制器的字符串名字
    NSString *targetVcName = forthDic[@"targetVcName"];
    
    //2.根据 名字  生成对应的类
    Class targetVcClass = NSClassFromString(targetVcName);
    
    //3.根据类生成 对象
    UIViewController *targertVc  =  [[targetVcClass alloc]init];
    
    //设置title
    targertVc.navigationItem.title = forthDic[@"title"];
    
    //判断 当前控制器是不是设置控制器
    if([targertVc isKindOfClass:[OPZJFatherTableViewController class]])
    {
        OPZJFatherTableViewController *settingVc = (OPZJFatherTableViewController *)targertVc;
        
        settingVc.plistName = forthDic[@"plistName"];
    }
    //4.跳转到对应的控制器
    if(targertVc){
        
        [self.navigationController pushViewController:targertVc animated:YES];
        
    }

}

-(void)leftBtnClick:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSArray array];
        NSString *path = [[NSBundle mainBundle] pathForResource:self.plistName ofType:nil];
        _dataArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _dataArray;
}

//数据源方法

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //第二级数据
    NSDictionary *secondDic = self.dataArray[section];
    
    //第三级数据
    NSArray *thirdArray = secondDic[@"items"];
    

    return thirdArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //第二级
    NSDictionary *secondDic  = self.dataArray[indexPath.section];
    
    //第三级
    NSArray *cellArray = secondDic[@"items"];
    
    //第四级
    NSDictionary *forthDic = cellArray[indexPath.row];
    //2.创建cell
    OPZJFatherTableViewCell *cell = [OPZJFatherTableViewCell fatherCellWithTableView:tableView withItems:forthDic];
                                     
    cell.items = forthDic;
    return cell;
}
//设置 tableVIew的 头标题 和尾标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return self.dataArray[section][@"header"];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return self.dataArray[section][@"footer"];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
